package kz.pvnhome.pimrestore;

import java.util.List;

import kz.pvnhome.pimrestore.pim.ContactDTO;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ContactListAdapter extends ArrayAdapter<ContactDTO> {
   private static final String TAG = "ContactListAdapter";

   private final LayoutInflater mInflater;

   public ContactListAdapter(Context context, List<ContactDTO> contacts) {
      super(context, 0, contacts);
      mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   }
   
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
         convertView = mInflater.inflate(R.layout.contacts_row, parent, false);
         Log.d(TAG, "INFLATE: pos=" + position); //$NON-NLS-1$
      }

      final ContactDTO contact = getItem(position);
      final TextView label = (TextView) convertView;

      label.setTag(contact);
      
      String name = "fam: " + contact.getFam();
      
      if (contact.getFam() == null) {
         name = "name: " + contact.getName();
      } else {
         name += ", name: " + contact.getName();
      }
      
      label.setText(name);

      Log.d(TAG, "TEXT: pos=" + position); //$NON-NLS-1$

      return convertView;
   }
}
